<template>
  <Form
    ref="formCom"
    :validation-schema="mySchema"
    v-slot="{ errors }"
    class="xtx-form"
    autocomplete="off"
  >
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-user"></i>
        <Field
          :class="{ err: errors.account }"
          v-model="form.account"
          name="account"
          class="Field"
          type="text"
          placeholder="请输入用户名"
        />
      </div>
      <div class="error" v-if="errors.account">{{ errors.account }}</div>
      <div class="error" v-if="errors.accountApi">{{ errors.accountApi }}</div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-phone"></i>
        <Field
          :class="{ err: errors.mobile }"
          v-model="form.mobile"
          name="phone"
          class="Field"
          type="text"
          placeholder="请输入手机号"
        />
      </div>
      <div class="error" v-if="errors.mobile">{{ errors.mobile }}</div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-code"></i>
        <Field
          :class="{ err: errors.code }"
          v-model="form.code"
          name="code"
          class="Field"
          type="text"
          placeholder="请输入验证码"
        />
        <span @click="send" class="code">{{
          time == 0 ? "发送验证码" : `${time}秒发送`
        }}</span>
      </div>
      <div class="error" v-if="errors.code">{{ errors.code }}</div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-lock"></i>
        <Field
          :class="{ err: errors.password }"
          v-model="form.password"
          name="password"
          class="Field"
          type="password"
          placeholder="请输入密码"
        />
      </div>
      <div class="error" v-if="errors.password">{{ errors.password }}</div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-lock"></i>
        <Field
          :class="{ err: errors.rePassword }"
          v-model="form.rePassword"
          name="rePassword"
          class="Field"
          type="password"
          placeholder="请确认密码"
        />
      </div>
      <div class="error" v-if="errors.rePassword">{{ errors.rePassword }}</div>
    </div>
    <a href="javascript:;" @click="submit" class="submit">立即提交</a>
  </Form>
</template>

<script>
</script>

<script setup>
// 引入表单校验组件
import { Form, Field } from 'vee-validate'
import { onUnmounted, reactive, ref } from 'vue'
// 表单校验规则
import schema from '@/utils/vee-validate'
import { userQQPathCode, userQQPatchLogin } from '@/api/user'
import Message from '@/components/library/Message'
import { useIntervalFn } from '@vueuse/shared'

import { useRouter } from 'vue-router'
import { useStore } from 'vuex'
export default {
  name: 'CallbackPatch',
  props: {
    unionId: {
      type: String,
      default: ''
    }
  }
}

// 获取表单数据对象
const form = reactive({
  account: null,
  mobile: null,
  code: null,
  password: null,
  rePassword: null
})
// 引入props
const props = defineProps({
  unionId: String
})
// 表单校验
const mySchema = {
  account: schema.accountApi,
  mobile: schema.mobile,
  code: schema.code,
  password: schema.password,
  rePassword: schema.rePassword
}

// 发送短信验证码
// pause 暂停 resume 开始
// useIntervalFn(回调函数,执行间隔,是否立即开启)
const formCom = ref(null)

const time = ref(0)
const { pause, resume } = useIntervalFn(
  () => {
    time.value--
    if (time.value <= 0) {
      pause()
    }
  },
  1000,
  false
)
onUnmounted(() => {
  // 关闭定时间
  pause()
})
// 发送短信验证 13666666666
const send = () => {
  const valid = mySchema.mobile(form.mobile)
  if (valid == true) {
    // 通过
    if (time.value === 0) {
      userQQPathCode(form.mobile).then(
        () => {
          Message({ type: 'success', text: '发送成功' })
          time.value = 60
          // 开启定时器
          resume()
        },
        () => {
          Message({ type: 'error', text: '手机号已绑定过其他账号' })
        }
      )
    }
  } else {
    formCom.value.setFieldError('mobile', valid)
  }
}

// 完善信息
const store = useStore()
const router = useRouter()
const submit = async () => {
  const valid = formCom.value.validate()
  if (valid) {
    userQQPatchLogin({
      unionId: props.unionId,
      ...form
    })
      .then((data) => {
        // 存储用户信息
        const { id, account, avatar, mobile, nickname, token } = data.result
        store.commit('user/setUser', {
          id,
          account,
          avatar,
          mobile,
          nickname,
          token
        })
        store.dispatch('cart/mergeCart').then(() => {
          // 2. 提示
          Message({ type: 'success', text: '登录成功' })

          // 进行跳转
          router.push(route.query.redirectUrl || '/')
        })
      })
      .catch((e) => {
        Message({ type: 'error', text: '完善信息失败' })
      })
  }
}
</script>

<style scoped lang="less">
.code {
  position: absolute;
  right: 0;
  top: 0;
  line-height: 50px;
  width: 80px;
  color: #999;
  &:hover {
    cursor: pointer;
  }
}

// 表单
.xtx-form {
  padding: 50px 0;
  &-item {
    display: flex;
    align-items: center;
    width: 700px;
    margin: 0 auto;
    padding-bottom: 25px;
    .label {
      width: 180px;
      padding-right: 10px;
      text-align: right;
      color: #999;
      background: rgb(20, 20, 20);
      ~ .field {
        margin-left: 0;
      }
    }
    .field {
      width: 320px;
      height: 50px;
      display: flex;
      align-items: center;
      margin-left: 190px;
      .icon {
        width: 40px;
        height: 50px;
        text-align: center;
        line-height: 50px;
        color: #999;
        ~ .input {
          padding-left: 40px;
        }
      }
      input {
        border: 1px solid #e4e4e4;
        width: 320px;
        height: 50px;
        line-height: 50px;
        padding: 0 10px;
        &.err {
          border-color: @priceColor;
        }
        &:focus,
        &:active {
          border-color: rgb(98, 221, 104);
        }
      }
    }
    .error {
      width: 180px;
      padding-left: 10px;
      color: @priceColor;
    }
  }
  .submit {
    width: 320px;
    height: 50px;
    border-radius: 4px;
    background: @xtxColor;
    height: 50px;
    line-height: 50px;
    text-align: center;
    font-size: 16px;
    color: #fff;
    display: block;
    margin: 0 auto;
  }
}
</style>
